import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { DoupiaopiaoUserEntity } from '../entity/user';
import * as _ from 'lodash';
import { CacheManager } from '@midwayjs/cache';
import { Context } from '@midwayjs/koa';

/**
 * 用户管理
 */
@Provide()
export class DoupiaopiaoUserService extends BaseService {
  @InjectEntityModel(DoupiaopiaoUserEntity)
  doupiaopiaoUserEntity: Repository<DoupiaopiaoUserEntity>;

  @Inject()
  cacheManager: CacheManager;

  @Inject()
  ctx: Context;

  /**
   * 根据ID获得信息
   * @param id
   */
  public async getUserinfoByClient() {
    const { userId } = this.ctx.app_userinfo;
    const info = await this.doupiaopiaoUserEntity.findOneBy({ id: userId });
    if (info) {
      delete info.password;
    }
    return info;
  }
  public async updatedInfo(query) {
    console.log(query, '------------');

    const { userId } = this.ctx.app_userinfo;
    const info = await this.doupiaopiaoUserEntity.findOneBy({ id: userId });
    if (info) {
      info.name = query.name;
      info.zfbCount = query.zfbCount;
      await this.doupiaopiaoUserEntity.save(info);
      return { msg: 'success' };
    } else {
      return { msg: 'error' };
    }
  }
}
